package view;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import main.ClusteringLingPipe;
import model.Doc;

public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public SearchServlet() {
        super();
    }

	public void init(ServletConfig config) throws ServletException {
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		out.println("testando get");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		PrintWriter out = response.getWriter();
		String query = request.getParameter("query");
		HashMap<String, Set<Doc>> clusters = main.ClusteringLingPipe.runSearch(query);
		
		Set<Doc> rawCluster = clusters.get(clusters.keySet().iterator().next());
		
		session.setAttribute("clusters", clusters);
		
		session.setAttribute("cluster", rawCluster );
		
//		out.println("<pre>");
//		out.println("A busca foi " + query);
//		out.println(clusters);
//		out.println("</pre>");
//		request.setAttribute("clusters", clusters);
		
		RequestDispatcher results = request.getRequestDispatcher("results.jsp");
		results.forward(request, response);
	}

}
